#include "base.h"

DB::DB()
: m_pDB(NULL)
{}

DB::~DB()
{}

int32 DB::Init()
{
	int32 nRt = -1;
	
	if(sqlite3_open(DB_PATH, &m_pDB) == SQLITE_OK)
	{
#ifdef DEBUG
	printf("init db ok\n");
#endif

		nRt = 0;
	}
	
	return nRt;
}

int32 DB::Close()
{
	sqlite3_close(m_pDB);
	m_pDB = NULL;
	
	return 0;
}

int32 DB::Update(const char8 *pStr)
{
	assert(pStr);
	
	int32 nRt = -1;
	

	char *msg;
	if(sqlite3_exec(m_pDB, pStr, NULL, NULL, &msg) != SQLITE_OK)
	{
		nRt = 0;
	}

#ifdef DEBUG
	printf("%s\n", msg);
#endif
	
	
	return nRt;
}

int32 DB::Query(const char8 *pStr, sqlite3_stmt **pStmt)
{
	assert(pStr);
	
	int32 nRt = -1;
	
	if(sqlite3_prepare(m_pDB, pStr, strlen(pStr), pStmt, NULL) == SQLITE_OK)
	{
	
#ifdef DEBUG
	printf("query ok: %s\n", pStr);
#endif

		nRt = 0;
	}


	return nRt;
}